package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.domain.model.LoginUser;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizGoodsBrowse;
import com.member.shop.service.ISysBizGoodsBrowseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 【商品浏览统计】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2025-01-11
 **/
@RestController
@RequestMapping("/shop/goodsBrowseStat")
public class SysBizGoodsBrowseController extends BaseController {
    @Resource
    private ISysBizGoodsBrowseService sysBizGoodsBrowseService;

    /**
     * 查询商品浏览统计列表
     */
    @PreAuthorize("@ss.hasPermi('shop:goodsBrowseStat:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizGoodsBrowse sysBizGoodsBrowse) {
        startPage();
        setTenantId(sysBizGoodsBrowse);
        List<SysBizGoodsBrowse> list = sysBizGoodsBrowseService.selectSysBizGoodsBrowseList(sysBizGoodsBrowse);
        return getDataTable(list);
    }

    private void setTenantId(SysBizGoodsBrowse sysBizGoodsBrowse) {
        LoginUser loginUser = getLoginUser();
        Long tenantId = loginUser.getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizGoodsBrowse.setTenantId(tenantId);
        }
    }

    /**
     * 导出商品浏览统计列表
     */
    @PreAuthorize("@ss.hasPermi('shop:goodsBrowseStat:export')")
    @Log(title = "商品浏览统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizGoodsBrowse sysBizGoodsBrowse) {
        setTenantId(sysBizGoodsBrowse);
        List<SysBizGoodsBrowse> list = sysBizGoodsBrowseService.selectSysBizGoodsBrowseList(sysBizGoodsBrowse);
        ExcelUtil<SysBizGoodsBrowse> util = new ExcelUtil<SysBizGoodsBrowse>(SysBizGoodsBrowse.class);
        util.exportExcel(response, list, "商品浏览统计数据");
    }

    /**
     * 获取商品浏览统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:goodsBrowseStat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizGoodsBrowseService.selectSysBizGoodsBrowseById(id));
    }

    /**
     * 新增商品浏览统计
     */
    @PreAuthorize("@ss.hasPermi('shop:goodsBrowseStat:add')")
    @Log(title = "商品浏览统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizGoodsBrowse sysBizGoodsBrowse) {
        setTenantId(sysBizGoodsBrowse);
        sysBizGoodsBrowse.setCreateTime(new Date());
        return toAjax(sysBizGoodsBrowseService.insertSysBizGoodsBrowse(sysBizGoodsBrowse));
    }

    /**
     * 修改商品浏览统计
     */
    @PreAuthorize("@ss.hasPermi('shop:goodsBrowseStat:edit')")
    @Log(title = "商品浏览统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizGoodsBrowse sysBizGoodsBrowse) {
        setTenantId(sysBizGoodsBrowse);
        sysBizGoodsBrowse.setUpdateTime(new Date());
        return toAjax(sysBizGoodsBrowseService.updateSysBizGoodsBrowse(sysBizGoodsBrowse));
    }

    /**
     * 删除商品浏览统计
     */
    @PreAuthorize("@ss.hasPermi('shop:goodsBrowseStat:remove')")
    @Log(title = "商品浏览统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizGoodsBrowseService.deleteSysBizGoodsBrowseByIds(ids));
    }
}
